﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace JiaBao.Core.Plugins
{
    /// <summary>
    /// 插件文件解析器(安装目录 说明文件)
    /// </summary>
    public class PluginFileParser
    {
        /// <summary>
        /// 解析目录文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static IList<string> ParseInstalledPluginsFile(string filePath)
        {
            //文件不存在
            if (!File.Exists(filePath))
                return new List<string>();

            //文件内容 空
            var text = File.ReadAllText(filePath);
            if (string.IsNullOrEmpty(text))
                return new List<string>();

            //逐行读取
            var lines = new List<string>();
            using (var reader = new StringReader(text))
            {
                string str;
                while ((str = reader.ReadLine()) != null)
                {
                    if (string.IsNullOrWhiteSpace(str))
                        continue;
                    lines.Add(str.Trim());
                }
            }
            return lines;
        }

        /// <summary>
        /// 保存目录文件
        /// </summary>
        /// <param name="pluginSystemNames"></param>
        /// <param name="filePath"></param>
        public static void SaveInstalledPluginsFile(IList<string> pluginSystemNames, string filePath)
        {
            string result = "";
            foreach (var sn in pluginSystemNames)
                result += string.Format("{0}{1}", sn, Environment.NewLine);
            File.WriteAllText(filePath, result);
        }

        /// <summary>
        /// 解析说明文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static PluginDescriptor ParsePluginDescriptorFile(string filePath)
        {
            var descriptor = new PluginDescriptor();

            //读取文件
            var text = File.ReadAllText(filePath);
            if (String.IsNullOrEmpty(text))
                return descriptor;

            var settings = new List<string>();
            using (var reader = new StringReader(text))
            {
                string str;
                while ((str = reader.ReadLine()) != null)
                {
                    if (string.IsNullOrWhiteSpace(str))
                        continue;
                    settings.Add(str.Trim());
                }
            }

            //解析配置
            foreach (var setting in settings)
            {
                var separatorIndex = setting.IndexOf(':');
                if (separatorIndex == -1)
                    continue;
                //读取 key value
                var key = setting.Substring(0, separatorIndex).Trim();
                string value = setting.Substring(separatorIndex + 1).Trim();
                switch (key)
                {
                    case "SystemName":
                        descriptor.SystemName = value;
                        break;

                    case "FriendlyName":
                        descriptor.FriendlyName = value;
                        break;

                    case "Descriptor":
                        descriptor.Descriptor = value;
                        break;

                    case "PluginFileName":
                        descriptor.PluginFileName = value;
                        break;
                    case "Version":
                        descriptor.Version = value;
                        break;
                    case "Author":
                        descriptor.Author = value;
                        break;

                    case "SupportedVersions":
                        descriptor.SupportedVersions = value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                                                            .Select(x => x.Trim())
                                                            .ToList();
                        break;

                    default:
                        //一些其他可用配置 有需要再添加
                        break;
                }
            }

            //如果没有 支持框架 默认加入支持 1.0
            if (!descriptor.SupportedVersions.Any())
                descriptor.SupportedVersions.Add("1.0");

            return descriptor;
        }

        /// <summary>
        /// 保存说明文件
        /// </summary>
        /// <param name="descriptor"></param>
        public static void SavePluginDescriptorFile(PluginDescriptor plugin)
        {
            if (plugin == null)
                throw new ArgumentException("plugin");


        }
    }
}
